
import axios from "../../utils/axios"



export default {
    namespaced:true,
    state(){
        return{
            tableData:[],

        }
    },
    mutations:{
        saveTableMutations(state,list){
            state.tableData = list
        },

    },
    actions:{
        getTableActions({commit}){
            axios({
                url:'api/getbanner',
                method:"get",
            }).then(response=>{
                commit('saveTableMutations',response.list)
            })
        },
        postTableActions({commit,dispatch},form){
             // 根据表单中是否包含id，切换 添加轮播图 或者 编辑轮播图
             let url = form.get("id")===null?"/api/banneradd":"/api/banneredit"
             console.log(url, "actions url")
             // 发送请求处理数据
             axios({
                 method: "POST",
                 url: url,
                 data: form
             }).then(response => {
                 if(response.code === 200) {
                     dispatch("getTableActions")
                 }
             })
        },
        deleteTableActions({commit,dispatch},id){
            axios({
                method  : "POST",
                url     : "/api/bannerdelete",
                data    : {id}
            }).then( response => {
                if(response.code === 200) {
                    dispatch("getTableActions")
                }
            })
        }
    },
    getters:{
        countBanners(state){

            return state.tableData.length
        }
    },


}

